package com.zengguang.testkaoshi.controller;


import com.zengguang.testkaoshi.eo.ClassEOPage;
import com.zengguang.testkaoshi.pojo.Class;
import com.zengguang.testkaoshi.services.impl.ClassService;
import com.zengguang.testkaoshi.tool.ResponseMessage;
import com.zengguang.testkaoshi.tool.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 *对外提供数据服务接口
 */
@Api(tags = "class")
@RestController
@CrossOrigin
public class ClassAction {
    @Autowired
    private ClassService service;

    @ApiOperation(value = "班级添加方法")
    @RequestMapping(value = "/addClass",method = RequestMethod.GET)
    public ResponseMessage add(Class cla){
        int isAdd=0;
        //传入数据的判断
        //(1)实体对象不能为空
        if (cla!=null) {
            //(2）班级名称不能为空
            if (cla.getClassname() != null) {
                //添加角色数据
                isAdd = service.insertClass(cla);
                if (isAdd==1){
                    return Result.success("保存成功");
                }else if (isAdd==-1){
                    return Result.error("班级重复不允许添加!");
                }else {
                    return Result.error("保存失败");
                }
            }
        }
        return Result.error("保存失败");
    }
    @ApiOperation(value = "班级删除方法")
    @RequestMapping(value = "/delClass",method = RequestMethod.GET)
    public ResponseMessage delClass(int classid){
        if (classid!=0){
            int delInt=0;
            delInt=service.deleteClass(classid);
            if (delInt>0){
                return Result.success("删除成功");
            }else {
                return Result.error("删除失败");
            }
        }else {
            return Result.error("删除id不能为0");
        }
    }

    @ApiOperation(value = "班级更新方法")
    @RequestMapping(value = "/updateClass",method = RequestMethod.GET)
    public ResponseMessage updateClass(Class cla){
        //传入参数不能为空
        if (cla!=null){
            //传入id不能为0
            if (cla.getClassid()!=0){
                if (service.updateClass(cla)==1){
                    return Result.success("更新成功");
                }else {
                    return Result.error("更新失败");
                }
              }
            }
          return Result.error("更新失败");
        }

        @ApiOperation(value = "查询全部")
        @RequestMapping(value = "/getAll",produces = "application/json;charset=utf-8",method = RequestMethod.PUT)
        public ResponseMessage getAll(ClassEOPage classEOPage){
            Map<String, List> map=new HashMap<>();
            List<ClassEOPage> list=service.getWhereAll(classEOPage);
            map.put("list",list);
            return Result.success("200",map);
        }

}
